let formatDate = (date)=>{
	if(!date){
		return
	}
	date = new Date(date);
	let m = date.getMonth() + 1
	let d = date.getDate()
	
	return `${date.getFullYear()}-${m.toString().padStart(2,'0')}-${d.toString().padStart(2,'0')}`
}
let getMonthStartDate = (date)=>{
	if(!date){
		return
	}
	date = new Date(date);
	let y = date.getFullYear()
	let m = date.getMonth()
	return new Date(y,m,1);
}
let getMonthEndDate = (date)=>{
	if(!date){
		return
	}
	date = new Date(date);
	let y = date.getFullYear()
	let m = date.getMonth() + 1
	let newDate =  new Date(y,m,1);
	return newDate.setDate(newDate.getDate() - 1)
}
let getCurrentDate = ()=>{
	return new Date()
}
let getCurrentMonthStartDate = ()=>{
	return getMonthStartDate(new Date())
}

let formatScmDate=(date) =>{
	if (!date) {
	  return null;
	}
	if (typeof date === "string") {
	  return date;
	}
	let format = '-';
	let year = date.getFullYear();
	let month = date.getMonth() + 1;
	let day = date.getDate();
	if (month < 10) {
	  month = '0' + month;
	}
	if (day < 10) {
	  day = '0' + day;
	}
	return year + format + month + format + day;
}
let formatDateTime=(date)=> {
	if (!date) {
	  return null;
	}
	if (typeof date === "string") {
	  return date;
	}
	date = new Date(date);
	let format = '-';
	let year = date.getFullYear();
	let month = date.getMonth() + 1;
	let day = date.getDate();
	let hours = date.getHours();
	let minutes = date.getMinutes();
	let seconds = date.getSeconds();
	if (month < 10) {
	  month = '0' + month;
	}
	if (day < 10) {
	  day = '0' + day;
	}
	if (hours < 10) {
	  hours = '0' + hours;
	}
	if (minutes < 10) {
	  minutes = '0' + minutes;
	}
	if (seconds < 10) {
	  seconds = '0' + seconds;
	}
	return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  }
  
let formatToTimestamp=(dateTime)=>{
    if (!dateTime) {
        return null;
    }
    const timestamp = new Date(dateTime).getTime().toString();
    return timestamp;
}

let getBirthDate=(value)=>{
	const birthYear=value.substring(6,10)
	const birthMonth=value.substring(10,12)
	const birthDay=value.substring(12,14)
	const birthDate=`${birthYear}-${birthMonth}-${birthDay}`
	return birthDate;
}
let  calculateAge=(birthday)=> {
	if (!birthday) return '-';
	const birthDate = new Date(birthday);
	const today = new Date();
	let age = today.getFullYear() - birthDate.getFullYear();
	const monthDiff = today.getMonth() - birthDate.getMonth();

	if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) {
	  age--;
	}

	return age;
}
let  getReminderDateRange=() =>{
	const currentDate = new Date();
	const pastDate = new Date();
	pastDate.setDate(currentDate.getDate() - 30);
  
	const formatDate = (date) => {
	  const month = (date.getMonth() + 1).toString().padStart(2, '0'); // Months are 0-based
	  const day = date.getDate().toString().padStart(2, '0');
	  return `${month}-${day}`;
	};
  
	return {
	  birthdayBegin: formatDate(pastDate),
	  birthdayEnd: formatDate(currentDate),
	};
  }
  let  calculateWorkYears=(hireDateString)=> {  
	if (!hireDateString) {
        return '';
      }

      const currentDate = new Date();
      const hireDate = new Date(hireDateString);

      let years = currentDate.getFullYear() - hireDate.getFullYear();
      let months = currentDate.getMonth() - hireDate.getMonth();
      let days = currentDate.getDate() - hireDate.getDate();

      if (days < 0) {
        months -= 1;
        days += new Date(currentDate.getFullYear(), currentDate.getMonth(), 0).getDate();
      }

      if (months < 0) {
        years -= 1;
        months += 12;
      }

      return `${years}年${months}月${days}天`;
  }
export default{
	formatDate,
	getMonthStartDate,
	getMonthEndDate,
	getCurrentDate,
	getCurrentMonthStartDate,
	formatScmDate,
	formatDateTime,
	formatToTimestamp,
	getBirthDate,
	calculateAge,
	getReminderDateRange,
	calculateWorkYears,
}